/**
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates.
 * The Universal Permissive License (UPL), Version 1.0
 */
"use strict";
define(["ojs/ojcore","jquery","ojs/ojdatasource-common","ojs/ojmodel"],function(a){a.tg=function(a,b,c,d,e){this.aO=a;this.fb=b;this.fY=[];this.AG=c;this.start=d<b.length?d:b.length-1;this.count=-1===e?b.length:Math.min(b.length,e)};o_("CollectionNodeSet",a.tg,a);a.tg.prototype.q0=function(a){this.W6(this).then(function(){a.success&&a.success()})};a.tg.prototype.W6=function(a){return new Promise(function(b){function c(e){e<d?a.jla(e,{success:function(b){null!==b?a.W6(b).then(function(){c(e+1)}):c(e+
1)}}):b(void 0)}var d=a.getCount();c(0)})};a.tg.prototype.jla=function(a,b){var c=this.fb.at(a);if(this.AG.aB(c).leaf)this.fY[a]=null,b.success(null);else{var d=this.AG.MG(c),c=this.AG.aB(c).key,e=this;this.AG.rB(d,0,-1,{success:function(c){e.fY[a]=c;b.success(c)}},c)}};a.tg.prototype.getParent=function(){return this.aO};a.b.g("CollectionNodeSet.prototype.getParent",{getParent:a.tg.prototype.getParent});a.tg.prototype.getStart=function(){return this.start};a.b.g("CollectionNodeSet.prototype.getStart",
{getStart:a.tg.prototype.getStart});a.tg.prototype.getCount=function(){return this.count};a.b.g("CollectionNodeSet.prototype.getCount",{getCount:a.tg.prototype.getCount});a.tg.prototype.getData=function(a){this.QR(a);return this.fb.at(a).attributes};a.b.g("CollectionNodeSet.prototype.getData",{getData:a.tg.prototype.getData});a.tg.prototype.QR=function(a){if(a<this.start||a>this.start+this.count)throw"Out of range";};a.tg.prototype.getMetadata=function(a){this.QR(a);var b={};a=this.fb.at(a);a=this.AG.aB(a);
b.key=a.key;b.leaf=a.leaf;b.depth=a.depth;return b};a.b.g("CollectionNodeSet.prototype.getMetadata",{getMetadata:a.tg.prototype.getMetadata});a.tg.prototype.ri=function(a){this.QR(a);return this.fY[a]};a.b.g("CollectionNodeSet.prototype.getChildNodeSet",{ri:a.tg.prototype.ri});a.lb=function(g){g=g||{};this.sG=g.root;this.HGa=g.childCollectionCallback;this.aB=g.parseMetadata;this.nx=null;this.iB="none";this.Rf={};a.lb.u.constructor.call(this)};o_("CollectionTreeDataSource",a.lb,a);a.lb.prototype.aB=
function(a){return{key:a.idAttribute+"\x3d"+a.id}};a.b.sa(a.lb,a.Iu,"oj.CollectionTreeDataSource");a.lb.prototype.Init=function(){a.lb.u.Init.call(this)};a.b.g("CollectionTreeDataSource.prototype.Init",{Init:a.lb.prototype.Init});a.lb.prototype.getChildCount=function(a){var b=this.Rf[a];if(b&&0<b.length)return b.length;this.HY(a,{success:function(a){return a.length}});return-1};a.b.g("CollectionTreeDataSource.prototype.getChildCount",{getChildCount:a.lb.prototype.getChildCount});a.lb.prototype.HY=
function(a,b){this.fetchChildren(a,null,{success:function(a){b.success(a.fb)},error:b.error})};a.b.g("CollectionTreeDataSource.prototype.getChildCollection",{HY:a.lb.prototype.HY});a.lb.prototype.fetchChildren=function(a,b,c){b=b||{};var d=b.start?b.start:0,e=b.count?b.count:-1;if(null===a)this.rB(null,d,e,c,null);else{var f=this;this.JT(this.sG,a,0).then(function(b){if(b){b=f.MG(b.Zt);try{f.rB(b,d,e,c,a)}catch(k){c&&c.error&&c.error({status:k.message})}}else c&&c.error&&c.error(a)})}};a.b.g("CollectionTreeDataSource.prototype.fetchChildren",
{fetchChildren:a.lb.prototype.fetchChildren});a.lb.prototype.Hla=function(a,b,c){var d=0;c&&c.at&&(d=c.at);b=this.LJ(b);a=this.CI(this,"insert",d,b,this.zba(null!=b&&0<b.length?b[b.length-1]:null,a));this.handleEvent("change",a)};a.lb.prototype.Ila=function(a,b,c){var d=0;c&&c.index&&(d=c.index);this.nCa(a);a=this.CI(this,"delete",d,this.LJ(b),null);this.handleEvent("change",a)};a.lb.prototype.Jla=function(a){var b=this.xua(a),c=null,d=null;b&&(c=b.index,d=this.LJ(b.fb));a=this.CI(this,"update",c,
d,this.zba(null!=d&&0<d.length?d[d.length-1]:null,a));this.handleEvent("change",a)};a.lb.prototype.$ka=function(a){a=this.CI(this,"refresh",null,this.LJ(a),null);this.handleEvent("refresh",a)};a.lb.prototype.zba=function(g,b){var c=new a.l;c.add(b);return this.x8(c,g,0,1)};a.lb.prototype.LJ=function(g){var b=[],c=null;do c=this.Dva(g),null!==c&&(c!==a.lb.W0&&b.unshift(c),g=this.yua(c));while(null!=c);return b};a.lb.W0="%!@ROOT%#@!";a.lb.prototype.mJ=function(g){var b=g instanceof a.C?this.aB(g).key:
g;return g?b:a.lb.W0};a.lb.prototype.O3=function(a){return this.Rf[this.mJ(a)]};a.lb.prototype.Vca=function(g,b){b.on(a.ka.O.ADD,this.Hla,this);b.on(a.ka.O.REMOVE,this.Ila,this);b.on(a.ka.O.CHANGE,this.Jla,this);b.on(a.ka.O.SYNC,this.$ka,this);this.Rf[this.mJ(g)]=b};a.lb.prototype.nCa=function(a){a=this.mJ(a);for(var b in this.Rf)if(this.Rf.hasOwnProperty(b)&&b===a){this.Rf[a].off(null,null,this);delete this.Rf[a];break}};a.lb.prototype.Pza=function(a,b){for(var c=b.length,d=0;d<c;d++){var e=this.mJ(b.at(d));
if(a===e)return!0}return!1};a.lb.prototype.xua=function(a){for(var b in this.Rf)if(this.Rf.hasOwnProperty(b))for(var c=this.Rf[b],d=0;d<c.length;d++)if(c.at(d)===a)return{index:d,fb:c};return null};a.lb.prototype.yua=function(a){for(var b in this.Rf)if(this.Rf.hasOwnProperty(b)){var c=this.Rf[b];if(this.Pza(a,c))return c}return null};a.lb.prototype.Dva=function(a){for(var b in this.Rf)if(this.Rf.hasOwnProperty(b)&&this.Rf[b]===a)return b;return null};a.lb.prototype.MG=function(a){var b=!0,c=this.O3(a);
c||(b=!1,c=this.HGa(this.sG,a),null!=c&&(this.q4(c),this.Vca(a,c)));return{fb:c,aY:b}};a.lb.prototype.CI=function(a,b,c,d,e){return{source:a,operation:b,index:c,parent:d,data:e}};a.lb.prototype.rB=function(a,b,c,d,e){var f=this;null===a&&((a=this.O3(null))?a={fb:a,aY:!0}:(a={fb:f.sG,aY:!1},f.Vca(null,this.sG)));a&&f.T6(a,function(a){d.success&&d.success(f.x8(a,e,b,c))},d.error)};a.lb.prototype.x8=function(g,b,c,d){return new a.tg(b,g,this,c,d)};a.lb.prototype.mDa=function(a,b){var c=this;return new Promise(function(d){function e(a,
b,g){a<b.length?b.at(a,{deferred:!0}).then(function(l){if(l){var m=c.aB(l);if(g===m.key){d(l);return}}a++;e(a,b,g)}):d(null)}e(0,a,b)})};a.lb.prototype.JT=function(a,b,c){var d=this;return new Promise(function(e){d.mDa(a,b).then(function(f){function h(d,f){if(d<k){var r=f.MG(a.at(d));r.fb?f.T6(r,function(a){f.JT(a,b,c+1).then(function(a){a?e(a):(d++,h(d,f))})},null):(d++,h(d,f))}else e(null)}if(f)e({Zt:f,depth:c});else{var k=a.length;h(0,d)}})})};a.lb.prototype.T6=function(a,b,c){a.aY?b(a.fb):(this.nx&&
"none"!==this.nx&&(a.fb.bm=this.nx,a.fb.G_=this.iB),0<a.fb.length||!a.fb.vla()?b(a.fb):a.fb.fetch({success:function(a){b(a)},error:c}))};a.lb.prototype.fetchDescendants=function(a,b){var c=this;null===a?this.rB(null,0,-1,{success:function(a){a.q0({success:function(){b.success&&b.success(a)}})}},null):this.JT(this.sG,a,0).then(function(d){d&&(d=c.MG(d.Zt),c.rB(d,0,-1,{success:function(a){a.q0({success:function(){b.success&&b.success(a)}})}},a))})};a.b.g("CollectionTreeDataSource.prototype.fetchDescendants",
{fetchDescendants:a.lb.prototype.fetchDescendants});a.lb.prototype.sort=function(a,b){var c=a.key,d=a.direction,e=!1;c!==this.nx&&(this.nx=c,e=!0);d!==this.iB&&(this.iB=d,e=!0);if(e){"none"===this.iB&&(this.Rf={});for(var f in this.Rf)this.Rf.hasOwnProperty(f)&&this.q4(this.Rf[f])}b&&b.success&&b.success()};a.b.g("CollectionTreeDataSource.prototype.sort",{sort:a.lb.prototype.sort});a.lb.prototype.q4=function(a){a.comparator=this.nx;a.sortDirection="ascending"===this.iB?1:-1;a.sort()};a.lb.prototype.getSortCriteria=
function(){return{key:this.nx,direction:this.iB}};a.b.g("CollectionTreeDataSource.prototype.getSortCriteria",{getSortCriteria:a.lb.prototype.getSortCriteria});a.lb.prototype.move=function(){a.p.kd()};a.b.g("CollectionTreeDataSource.prototype.move",{move:a.lb.prototype.move});a.lb.prototype.moveOK=function(){return"invalid"};a.b.g("CollectionTreeDataSource.prototype.moveOK",{moveOK:a.lb.prototype.moveOK});a.lb.prototype.getCapability=function(a){return"sort"===a?"default":"move"===a?"none":"batchFetch"===
a||"fetchDescendants"===a?"disable":null};a.b.g("CollectionTreeDataSource.prototype.getCapability",{getCapability:a.lb.prototype.getCapability})});